package com.jcodecraeer.xrecyclerview;

import com.jcodecraeer.xrecyclerview.progressindicator.AVLoadingIndicatorView;
import com.jcodecraeer.xrecyclerview.utils.DeviceUtils;
import com.jcodecraeer.xrecyclerview.utils.ResUtil;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;
import ohos.app.Context;

import static ohos.agp.utils.LayoutAlignment.CENTER;

public class LoadingMoreFooter extends DirectionalLayout {

    public final static int STATE_LOADING = 0;
    public final static int STATE_COMPLETE = 1;
    public final static int STATE_NOMORE = 2;

    private AVLoadingIndicatorView progressView;
    private SimpleViewSwitcher progressCon;
    private Component mComponent;

    private Text mText;
    private String loadingHint;
    private String noMoreHint;
    private String loadingDoneHint;

    public LoadingMoreFooter(Context context) {
        super(context);
        initView(context, null, null);
    }

    public LoadingMoreFooter(Context context, AttrSet attrSet) {
        super(context, attrSet);
        initView(context, attrSet, null);
    }

    public LoadingMoreFooter(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        initView(context, attrSet, styleName);
    }

    private void initView(Context context, AttrSet attrSet, String styleName){

        setAlignment(CENTER);
        setOrientation(HORIZONTAL);
        setComponentSize(LayoutConfig.MATCH_PARENT, DeviceUtils.dp2px(getContext(),120));
        progressCon = new SimpleViewSwitcher(context);
        progressCon.setWidth(DeviceUtils.dp2px(getContext(),40));
        progressCon.setHeight(DeviceUtils.dp2px(getContext(),40));
        addComponent(progressCon);
        addText();
    }

    public void setProgressStyle(int style) {
        progressView = new  AVLoadingIndicatorView(getContext());
        if(style == ProgressStyle.SysProgress){
            progressView.setIndicatorColor(Color.RED.getValue());
        }else {
            progressView.setIndicatorColor(0xffB5B5B5);
        }
        progressView.setIndicatorId(style);
        progressCon.setView(progressView);
        progressView.applyAnimation();
    }

    public void setIndicatorColor(int color){
        progressView.setIndicatorColor(color);
        this.invalidate();
    }

    public int getVisibleHeight(){
        LayoutConfig layoutConfig = new LayoutConfig();
        return layoutConfig.height;
    }

    public void  setState(int state) {
        switch(state) {
            case STATE_LOADING:
                progressCon.setVisibility(VISIBLE);
                mText.setText(loadingHint);
                this.setVisibility(VISIBLE);
                break;
            case STATE_COMPLETE:
                mText.setText(loadingDoneHint);
                this.setVisibility(HIDE);
                break;
            case STATE_NOMORE:
                mText.setText(noMoreHint);
                progressCon.setVisibility(HIDE);
                this.setVisibility(VISIBLE);
                break;
        }
    }
    public void destroy(){
        progressCon = null;
        if(progressView != null){
            progressView.destroy();
            progressView = null;
        }
    }

    public void setProgressVisibility(int visible){
        progressCon.setVisibility(visible);
    }
    public void setLoadingHint(String hint) {
        loadingHint = hint;
    }

    public void setNoMoreHint(String hint) {
        noMoreHint = hint;
    }

    public void setLoadingDoneHint(String hint) {
        loadingDoneHint = hint;
    }

    private void addText(){
        mText = new Text(getContext());
        mText.setText(ResUtil.getString(getContext(),ResourceTable.String_xr_listview_loading));

        if(loadingHint == null || loadingHint.equals("")){
            loadingHint = ResUtil.getString(getContext(),ResourceTable.String_xr_listview_loading);
        }

        if(noMoreHint == null || noMoreHint.equals("")){
            noMoreHint = ResUtil.getString(getContext(),ResourceTable.String_xr_nomore_loading);
        }

        if(loadingDoneHint == null || loadingDoneHint.equals("")){
            loadingDoneHint = ResUtil.getString(getContext(),ResourceTable.String_xr_loading_done);
        }

        LayoutConfig layoutParams = new LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT,
                ComponentContainer.LayoutConfig.MATCH_CONTENT);
        layoutParams.setMargins(DeviceUtils.dp2px(getContext(),5),0,0,0);
        mText.setLayoutConfig(layoutParams);
        mText.setTextAlignment(TextAlignment.CENTER);
        mText.setTextSize(DeviceUtils.dp2px(getContext(),16));
        addComponent(mText);
    }
}
